//
//  LLT_SearchPlayerViewController_LXM.m
//  CoderLLT
//
//  Created by 刘晓萌 on 15/12/11.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_SearchPlayerViewController_LXM.h"
#import <AVOSCloud/AVOSCloud.h>
#import "UIViewController+LLT_LoginAndRegist_LXM.h"
#import "UIViewController+LLT_UIViewControllerTools.h"
#import "LLT_DataHelper_LXM.h"
#import "LLT_PlayerModel.h"
#import "LLT_TabBarController_LXM.h"
#import "LLT_PlayerInfoViewController_LXM.h"
#import "LLT_AddPlayerInfoViewController_LXM.h"
#import "LLT_VideoViewController_LXH.h"
#import "LLT_DataViewController_LXM.h"
#import "LLT_NewsViewController_LXH.h"

@interface LLT_SearchPlayerViewController_LXM ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *servernNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *userNameLabel;
@property (strong,nonatomic) NSArray *serverArr;
@property (strong,nonatomic) UIPickerView *serverNamePickerView;
@property (strong,nonatomic) LLT_PlayerModel *player;

@end

@implementation LLT_SearchPlayerViewController_LXM

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark --- 初始化页面
-(void)makeView{

    self.serverArr = [self getServerArr];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickServerNameLabel:)];
    [self.servernNameLabel addGestureRecognizer:tap];
    self.servernNameLabel.userInteractionEnabled = YES;
    [self addPicView];
    self.serverNamePickerView.hidden = YES;
    
}

#pragma mark ---点击事件
/**
 * 点击绑定按键
 */
- (IBAction)didClickBindingButton:(id)sender {
    //搜索账号 -- 存储用户手机号 跟 玩家名和玩家服务器
    LLT_DataHelper_LXM *dh = [LLT_DataHelper_LXM new];
    __weak typeof(self) weakSelf = self;
    dh.setPlayerInfo = ^(LLT_PlayerModel *player){
        if (player.playerName.length == 0) {
            [weakSelf showAlert:@"提 示" andAlertString:@"请输入正确的用户信息"];
        }else{
            weakSelf.player = player;
            [weakSelf getTabBarWithBrinding];
            [weakSelf saveToAVOS:player];
            [weakSelf showAlert:@"绑定成功" andAlertString:@"" andHandler:^(UIAlertAction *action) {
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }];
        }
    };
    [dh getPlayerDetailByPlayerName:self.userNameLabel.text andServerName:self.servernNameLabel.text];
    //NSLog(@"绑定成功");
}

/**
 * 设置tabBarController
 */
-(void)getTabBarWithBrinding{
    
    LLT_TabBarController_LXM *tabbarController = [LLT_TabBarController_LXM shareTabBar];
    LLT_DataViewController_LXM  *dataVC = getVC(@"dataView_Table");
    LLT_PlayerInfoViewController_LXM  *playVC = getVC(@"playerInfoVC");
    playVC.player = self.player;
    LLT_NewsViewController_LXH  *newsVC = getVC(@"newsVC");
    LLT_VideoViewController_LXH  *lVVC = getVC(@"liveAndVideoVC");
    
    //此处添加判定条件(根据是否绑定游戏角色来判断)
    NSArray *arr = @[playVC,dataVC,newsVC,lVVC];
    [tabbarController setViewControllers:arr];
    
    DissMissBack;
}

/**
 * 保存信息到云端
 */
-(void)saveToAVOS:(LLT_PlayerModel *)player{
    AVObject *post = [AVObject objectWithClassName:@"LLT_PlayerInfo"];
    [post setObject:self.userNameLabel.text forKey:@"pname"];
    [post setObject:self.servernNameLabel.text forKey:@"pserverName"];
    AVUser *user = [AVUser currentUser];
    [post setObject:user.mobilePhoneNumber forKey:@"phoneNumber"];
    [post save];
}

/**
 * 点击左返回按钮
 */
- (IBAction)didClickLeftBackButton:(UIButton *)sender {
    DissMissBack;
}

- (void)didClickServerNameLabel:(UITapGestureRecognizer *)sender{
    self.serverNamePickerView.hidden = !self.serverNamePickerView.hidden;
}

#pragma mark --- 功能模块

/**
 * 添加serverArr
 */
-(NSArray *)getServerArr{
    NSDictionary *serverDic = @{@"艾欧尼亚":@"1",
                   @"祖安":@"3",
                   @"诺克萨斯":@"4",
                   @"班德尔城":@"5",
                   @"皮尔特沃夫":@"7",
                   @"战争学院":@"8",
                   @"巨神峰":@"10",
                   @"雷瑟守备":@"11",
                   @"裁决之地":@"13",
                   @"黑色玫瑰":@"14",
                   @"暗影岛":@"15",
                   @"钢铁烈阳":@"17",
                   @"水晶之痕":@"18",
                   @"均衡教派":@"19",
                   @"影流":@"22",
                   @"守望之海":@"23",
                   @"征服之海":@"24",
                   @"卡拉曼达":@"25",
                   @"皮城警备":@"27",
                   @"比尔吉沃特":@"2",
                   @"德玛西亚":@"6",
                   @"弗雷尔卓德":@"9",
                   @"无畏先锋":@"12",
                   @"恕瑞玛":@"16",
                   @"扭曲丛林":@"20",
                   @"巨龙之巢":@"26",
                   @"教育网专区":@"21"};
    
    NSArray *arr = [serverDic allKeys];
    return arr;
}

/**
 * 添加picker选择区
 */
-(void)addPicView{
    // UIPickerView
    UIPickerView *serverNamePickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,126, WIDTH,120)];
    //处理代理方法
    serverNamePickerView.dataSource = self;
    serverNamePickerView.delegate = self;
    //设置背景颜色
    serverNamePickerView.backgroundColor = [UIColor lightGrayColor];
    self.serverNamePickerView = serverNamePickerView;
    [self.view addSubview:self.serverNamePickerView ];
}


#pragma mark --- pickerview 代理方法
// pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.serverArr.count;
}
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 160;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    self.servernNameLabel.text = self.serverArr[row];
    return self.serverArr[row];
}

@end
